<?php
/**
 * 申通留言系统接口
 * @author ljp
 */

namespace soa\sto;

use soa\client;
use soa\check\EnvCheck;

class mcg extends client{

    protected $service_name = 'sto_mcg';
    protected $_timeout = 2;

    public $service_url = 'http://api.dev.liuyan.kuaidihelp.com/rest';//81和测试环境使用,上线请注释,切换线上
    //public $service_url = 'http://api.liuyan.kuaidihelp.com/rest';//线上正式环境使用

    private $_app_key = '@#Co29s)la8dn_d';
    private $_app_id = '1000';
	private $_envCheck;
	
	public function __construct(){
		//$envObj = new EnvCheck();
		$this->_envCheck = EnvCheck::isEnvVPC();
		$curEnv = EnvCheck::getCurrentEnv();
		if(in_array($curEnv,array('ENV_ONLINE','ENV_VPC'))){
			$this->service_url = 'http://api.liuyan.kuaidihelp.com/rest';
		}
	}

    public function request_params($api,$data) {

        $c = explode('/',$api);
        if(count($c) > 2){
            array_shift($c);
            $api = join('/',$c);
        }

        $time = time();
        $req = array(
            'data' => json_encode($data),
            'ts' => $time,
            'app_id' => $this->_app_id,
            'sign' => $this->_sign($time,$api),
            '_client_name_' => 'kd_message'
        );
        return $req;
    }

    private function _sign($time,$api) {
        return md5($time . $this->_app_key . $api .$this->_app_id);
    }

    public function message_query($filter = array(), $page = 1, $page_size = 20) {

        $args = array('p' => $page, 'pSize' => $page_size);
        if($filter){
            $args += $filter;
        }
        return $this->get('message/query', $args);
    }
	
	public function searchByNo($data){
		return $this->get('message/message/searchByNo',$data);
	}

    public function message_info($args) {
        return $this->get('message/info', $args);
    }

    public function message_add($data)  {
		if($this->_envCheck){
			return array();
			exit();
		}
        return $this->post('message/add',$data);
    }
	
	//问题件登记
	public function problem_add($data){
		if($this->_envCheck){
			return array();
			exit();
		}
		return $this->post('message/ai/add',$data);
	}
	
	public function pic_add($data){
		if($this->_envCheck){
			return array();
			exit();
		}
		return $this->post('message/picUp',$data);
	}

    public function message_reply_list($data){
        return $this->get('message/reply/list',$data);
    }

    public function message_reply_add($data){
		if($this->_envCheck){
			return array();
			exit();
		}
        return $this->post('message/reply/add',$data);
    }

    public function message_read($data){
        return $this->post('message/read',$data);
    }

    public function query_branch($data){
        return $this->post('message/queryBranch',$data);
    }

    public function auto_search($data){
        return $this->post('message/autoSearch',$data);
    }
	
	public function assign($data){
		if($this->_envCheck){
			return array();
			exit();
		}
		return $this->post('message/message/assign',$data);
	}

    //用户留言
    public function feedback_add($data = array()){
		if($this->_envCheck){
			return array();
			exit();
		}
        return $this->post('feedback/add', $data);
    }

    public function feedback_query($data = array()){
        return $this->post('feedback/query', $data);
    }

    public function feedback_reply_list($data){
        return $this->post('feedback/reply/list',$data);
    }

    public function feedback_reply_add($data){
		if($this->_envCheck){
			return array();
			exit();
		}
        return $this->post('feedback/reply/add',$data);
    }

    public function is_reg($userno,$username,$is_return='no'){
        return $this->post('member/is_reg',array('userno'=>$userno,'username'=>$username,'is_return'=>$is_return));
    }
	
	public function memberPriv($userno){
		return $this->post('member/memPriv',array('userno'=>$userno));
	}
	
	public function orgMember($userno){
		return $this->post('member/member/orgMember',array('userno'=>$userno));
	}
	
	public function childOrgList($org_code){
		//logDebug($org_code,'child1');
		return $this->post('member/member/childOrgList',array('org_code'=>$org_code));
	}

    public function isExistMsg($no){
        return $this->post('message/isExistMsg',array('no'=>$no));
    }

    /******************  collection 类所需  ********************/

    public function topic_list_request($data){
        return $this->_request_api_url('message/query',$data);
    }

    public function reply_list_request($data){
        return $this->_request_api_url('message/reply/query',$data);
    }

    public function unread_count_request($data){
        return $this->_request_api_url('message/unread',$data);
    }

    private function _request_api_url($api,$data = array()){
        $data = $this->request_params($api, $data);
        return $this->request_api($api).'?'.http_build_query($data);
    }
}
